package chat;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.List;

public class ServerReceive implements Runnable{
	private ChatRecord<String> chatRecord;
	private Socket socket;
	private List<Socket> Socket_List;
	public ServerReceive(Socket socket,ChatRecord<String> chatRecord, List<Socket> Socket_List) {
		this.socket=socket;
		this.chatRecord=chatRecord;
		this.Socket_List=Socket_List;
	}
	
	@Override
	public void run() {
		try {
			while(true){
				ObjectInputStream ois=new ObjectInputStream(socket.getInputStream());
				String o=(String)ois.readObject();
				chatRecord.add(socket.getInetAddress()+">>"+o);
			//	sendMessage(socket.getInetAddress()+">>"+o);
			}	
		} catch (Exception e) {
			Socket_List.remove(socket);
			chatRecord.add(socket.getInetAddress()+"离开了");
			//sendMessage(socket.getInetAddress()+"离开了");
		}
			
	}

/*	private void sendMessage(String o) {
		try {
			for (Socket socket : Socket_List) {
				ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
				oos.writeObject(o);
				oos.flush();
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
	}*/
	
}
